<?php
/*
 * (C) Copyright 2013 Torsten Gorath (http://www.blauesdach.de/).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Contributors:
 *     Torsten Gorath
 */
require_once $basedir."/inc/table.php";

class User extends table {
	private $vorname = "";
	private $nachname = "";
	private $email = "";
	private $password = "";

    public function __construct($id="") {
		if($id!='') {
	        $query = "SELECT * FROM User WHERE email='" . $id . "';";
	        $result = Database::getDefault()->query($query);
	        $temp = mysql_fetch_object($result, "User");
            foreach($temp as $key => $value) {
                $this->$key = $value;
            }
		}
    }
    
    public function delete() {
        Database::getDefault()->delete(get_class($this),$this->id);
    }
    
    public function save() {
    	$values = array();
		foreach ($this as $key => $value) {
			$values[$key] = $value;
		}
        if($this->email=="") {
	        $datas = array_map('mysql_real_escape_string', array_values($values));
	        $keys = array_keys($values);
	        $query = 'INSERT INTO `'.$table.'` (`'.implode('`,`', $keys).'`) VALUES (\''.implode('\',\'', $datas).'\')';
	        $result = $this->query($query);
	        if(!$result) {
	            throw new Exception("Can't insert object into database", 1);
	        }
        } else {
	        $query = "UPDATE User SET ";
	        $i = 0;
	        foreach($values as $key => $value) {
	        	$i++;
	            $query .= $key."='".$value."'";
				if($i<count($values)) {
					$query .= ", ";
				}
	        }
	        $query .= " WHERE email='".$this->email."'";
	        $result = Database::getDefault()->query($query);
	        if(!$result) {
	            throw new Exception("Can't update object in database", 1);
	        }
        }
    }
    
    static public function getAll() {
        $query = "SELECT * FROM User";
        return Database::getDefault()->execute($query,"User");
    }

	public function setVorname($value) {
		$this->vorname = $value;
	}
	
	public function getVorname() {
		return $this->vorname;
	}
	
	public function setNachname($value) {
		$this->nachname = $value;
	}
	
	public function getNachname() {
		return $this->nachname;
	}
	
	public function setEmail($value) {
		$this->email = $value;
	}
	
	public function getEmail() {
		return $this->email;
	}
	
	public function setPassword($value) {
		$this->password = md5($value);
	}
	
	public function login($email, $password) {
		
	}

}
?>